package com.mystudy.reflect;

import org.junit.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @program: infoalgorithm
 * @description: 场景类
 * @author: zhouzhilong
 * @create: 2019-04-23 16:16
 **/
public class Demo {

    /**
     * 获取类的公有属性,私有属性,公有方法,私有方法
     *
     * @throws NoSuchFieldException
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    @Test
    public void testReflect() throws NoSuchFieldException, IllegalAccessException, InstantiationException {
        Class<User> userClass = User.class;
        String className = userClass.getName();
        System.out.println("className:" + className);

        //获取public属性
        Field[] pubFields = userClass.getFields();
        for (Field field : pubFields) {
            System.out.println(field.getModifiers() + ":" + field.getName());
        }

        //获取全部属性
        Field[] allFields = userClass.getDeclaredFields();
        for (Field field : allFields) {
            System.out.println(field.getModifiers() + "--" + field.getName());
        }

        //获取public方法
        Method[] methods = userClass.getMethods();
        for (Method method : methods) {
            System.out.println(method.getModifiers() + ":" + method.getName());
        }

        //获取全部方法
        Method[] allMethods = userClass.getDeclaredMethods();
        for (Method method : allMethods) {
            System.out.println(method.getModifiers() + ":" + method.getName());
        }

        //获取指定public属性
        Field height = userClass.getField("height");
        System.out.println(height);

        //获取指定私有属性
        Field age = userClass.getDeclaredField("age");
        age.setAccessible(true);
        System.out.println(age);

        //创建对象
        User user = userClass.newInstance();
        //设置值,注意此处操作,不是使用的新创建的对象
        age.set(user, 18);
        System.out.println(user.getAge());

        //获取构造方法
        Constructor<?>[] constructors = userClass.getConstructors();
        for (Constructor constructor : constructors) {
            System.out.println(constructor.toString());
        }
    }

    @Test
    public void testGeneric() throws IllegalAccessException, InstantiationException {
        Class<Son> sonClass = Son.class;
        Field field = null;
        //获取父类私有属性值
        Son son = sonClass.newInstance();
        for (Class c = sonClass.getSuperclass(); c != Object.class; c = c.getSuperclass()) {
            try {
                field = c.getDeclaredField("name");
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
            field.setAccessible(true);
        }
        System.out.println(field.get(son));
    }

    @Test
    public void testParams(){
        try {
            Class  clazz = Class.forName(Parent.class.getName());
            Constructor constructor = clazz.getConstructor(String.class, int.class);
            Parent parent = (Parent) constructor.newInstance("呵呵",34);
            System.out.println(parent);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }



}
